<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="name">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="name">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请码" prop="name">
        <el-input
          v-model="queryParams.inviteCode"
          placeholder="请输入邀请码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="userId" />
      <el-table-column align="center" label="头像" prop="avatar">
        <template #default="scope">
          <img :src="scope.row.avatar" style="width: 40px" />
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.USER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="可用积分" align="center" prop="points" />
      <el-table-column label="邀请码" align="center" prop="inviteCode" />
      <el-table-column label="最后一次登录时间" align="center" prop="loginDate" :formatter="dateFormatter"/>
      <el-table-column
        label="注册时间"
        align="center"
        prop="regTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="佣金" align="center" prop="totalCommission" />
      <el-table-column label="可提现金额" align="center" prop="withdrawAvailable" />
      <el-table-column label="已提现金额" align="center" prop="totalWithdrawSucc" />
      <el-table-column label="可退款金额" align="center" prop="refundAvailable" />
      <el-table-column label="累计充值金额" align="center" prop="packageTotalPrice" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
         <!-- <el-button link type="primary" @click="openDetail(scope.row.userId)">详情</el-button>-->
          <el-dropdown
            @command="(command) => handleCommand(command, scope.row)"
          >
            <el-button link type="primary">
              <Icon icon="ep:d-arrow-right" />
              更多
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item
                  command="handleUpdatePoint"
                >
                  修改积分
                </el-dropdown-item>

                <el-dropdown-item
                  command="handleOrder"
                >
                  购买记录
                </el-dropdown-item>
                <el-dropdown-item
                  command="handlePromotion"
                >
                  邀请记录
                </el-dropdown-item>
                <el-dropdown-item
                  command="handleExam"
                >
                  笔试记录
                </el-dropdown-item>
                <el-dropdown-item
                  command="handleWithDraw"
                >
                  提现记录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <UserPointUpdateForm ref="updatePointFormRef" @success="getList" />
  <UserOrderListForm ref="userOrderListFormRef" />
  <UserPromotionListForm ref="userPromotionListFormRef" />
  <UserExamListForm ref="userExamListFormRef" />
  <UserWithdrawListForm ref="userWithDrawListFormRef" />
</template>

<script setup lang="ts" name="MemberTag">
  import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
  import UserPointUpdateForm from './components/UserPointUpdateForm.vue'
  import UserOrderListForm from './components/UserOrderList.vue'
  import UserPromotionListForm from './components/UserPromotionList.vue'
  import UserExamListForm from './components/UserExamList.vue'
  import UserWithdrawListForm from './components/UserWithdrawList.vue'
import * as WrittenApi from '@/api/member/written'
  import * as UserApi from "@/api/member/user";
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  nickname: '',
  mobile: '',
  inviteCode: '',
})
const queryFormRef = ref() // 搜索的表单

/** 打开会员详情 */
const { push } = useRouter()
const openDetail = (id: number) => {
  push({ name: 'MemberUserDetail', params: { id } })
}
const updatePointFormRef = ref() // 修改会员积分表单
const userOrderListFormRef = ref() // 修改会员积分表单
const userPromotionListFormRef = ref() // 修改会员积分表单
const userExamListFormRef = ref() // 修改会员积分表单
const userWithDrawListFormRef = ref() // 修改会员积分表单
/** 操作分发 */
const handleCommand = (command: string, row: UserApi.UserVO) => {
  switch (command) {
    case 'handleUpdatePoint':
      updatePointFormRef.value.open(row.userId,row.points)
      break
    case 'handleOrder':
      userOrderListFormRef.value.open(row.userId)
      break
    case 'handlePromotion':
      userPromotionListFormRef.value.open(row.userId)
      break
    case 'handleExam':
      userExamListFormRef.value.open(row.userId)
      break
    case 'handleWithDraw':
      userWithDrawListFormRef.value.open(row.userId)
      break
    default:
      break
  }
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WrittenApi.getMemberPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
